package net.maku.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.system.convert. SysContractTypeConvert;
import net.maku.system.dao. SysContractTypeDao;
import net.maku.system.entity. SysContractTypeEntity;
import net.maku.system.query. SysContractTypeQuery;
import net.maku.system.service. SysContractTypeService;
import net.maku.system.vo. SysContractTypeVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 附件管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
@AllArgsConstructor
public class SysContractTypeServiceImpl extends BaseServiceImpl< SysContractTypeDao,  SysContractTypeEntity> implements  SysContractTypeService {

    @Override
    public PageResult< SysContractTypeVO> page( SysContractTypeQuery query) {
        IPage<SysContractTypeEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>( SysContractTypeConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SysContractTypeEntity> getWrapper( SysContractTypeQuery query) {
        LambdaQueryWrapper<SysContractTypeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(query.getTypeGroup()),  SysContractTypeEntity::getTypeGroup, query.getTypeGroup());
        wrapper.eq(StrUtil.isNotBlank(query.getTypeCode()),  SysContractTypeEntity::getTypeCode, query.getTypeCode());
        wrapper.like(StrUtil.isNotBlank(query.getTypeName()),  SysContractTypeEntity::getTypeName, query.getTypeName());
        wrapper.orderByDesc( SysContractTypeEntity::getId);
        return wrapper;
    }

    @Override
    public void save( SysContractTypeVO vo) {
         SysContractTypeEntity entity =  SysContractTypeConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update( SysContractTypeVO vo) {
         SysContractTypeEntity entity =  SysContractTypeConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}